/*
* Copyright 2016 Yonyou Auto Information Technology（Shanghai） Co., Ltd. All Rights Reserved.
*
* This software is published under the terms of the YONYOU Software
* License version 1.0, a copy of which has been included with this
* distribution in the LICENSE.txt file.
*
* @Project Name : marketing-common
*
* @File name : RandomCodeUtil.java
*
* @Author : WangHuicheng
*
* @Date : 2016年11月29日
*
----------------------------------------------------------------------------------
*     Date       Who       Version     Comments
* 1. 2016年11月29日    WangHuicheng    1.0
*
*
*
*
----------------------------------------------------------------------------------
*/
	
package org.marketing.common.util;

import java.awt.image.BufferedImage;
import java.io.OutputStream;
import java.util.Properties;

import javax.imageio.ImageIO;

import com.google.code.kaptcha.Producer;
import com.google.code.kaptcha.util.Config;

/**
* 短信验证码生成
* @author WangHuicheng
* @date 2016年11月29日
*/

public class RandomCodeUtil {
    private static RandomCodeUtil instance;
    private Producer producer;
    
    private RandomCodeUtil(Properties prop){
        Config config = new Config(prop);
        producer = config.getProducerImpl();
    }
    
    public static RandomCodeUtil defaultInstance(){
        return instance == null?(instance = new RandomCodeUtil(new Properties())):instance;
    }
    
    public static RandomCodeUtil buildInstance(Properties prop){
        return new RandomCodeUtil(prop);
    }
    
    /**
     * 
     * <p>Description:生成随机校验码 </p>
     */
    public String genRandomCode(){
        return producer.createText();
    }
    
    /**
     * 
     * <p>Description: 写入生成的校验码图片</p>
     * @param code
     * @param outputStream
     */
    public void writeImageTo(String code,OutputStream outputStream)throws Exception{
        BufferedImage createImage = producer.createImage(code);
        ImageIO.write(createImage, "jpg", outputStream);
    }
}
